package com.thuanpq60023.easyenglish.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.thuanpq60023.easyenglish.R;
import com.thuanpq60023.easyenglish.bus.UnitBus;
import com.thuanpq60023.easyenglish.bus.VocabularyBus;
import com.thuanpq60023.easyenglish.entity.UnitEntity;
import com.thuanpq60023.easyenglish.entity.VocabEntity;
import com.thuanpq60023.easyenglish.helper.GlobalFont;
import com.thuanpq60023.easyenglish.view.Header;

public class TestActivity extends Activity implements OnClickListener {
	private UnitBus mUnitBus;
	private Spinner spnUnit;
	private VocabularyBus mVocabularyBus;
	private Button btnStart;
	private Button btnOption1;
	private Button btnOption2;
	private Button btnOption3;
	private Button btnOption4;
	private TextView tvWrong;
	private TextView tvRight;
	private TextView tvWrongTitle;
	private TextView tvRightTitle;
	private TextView tvQuestion;
	private VocabEntity[] mRandomVocabs;
	private VocabEntity currentVocab;
	private String strAnswer = "";
	private AlertDialog mAlertDialog;
	private int right = 0;
	private int wrong = 0;
	private Typeface font1;
	private VocabEntity[] mAllVocabs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_layout);
		initChild();
		initView();
		initHeader();
		loadUnit();
	}

	public void initView() {
		tvWrongTitle = (TextView) findViewById(R.id.test_layout_tv_wrong_title);
		tvWrongTitle.setTypeface(font1);
		tvRightTitle = (TextView) findViewById(R.id.test_layout_tv_right_title);
		tvRightTitle.setTypeface(font1);
		tvWrong = (TextView) findViewById(R.id.test_layout_tv_wrong);
		tvRight = (TextView) findViewById(R.id.test_layout_tv_right);
		spnUnit = (Spinner) findViewById(R.id.test_layout_spn_unit);
		btnStart = (Button) findViewById(R.id.test_layout_btn_start);
		btnOption1 = (Button) findViewById(R.id.test_layout_option1);
		btnOption1.setTypeface(font1);
		btnOption2 = (Button) findViewById(R.id.test_layout_option2);
		btnOption2.setTypeface(font1);
		btnOption3 = (Button) findViewById(R.id.test_layout_option3);
		btnOption3.setTypeface(font1);
		btnOption4 = (Button) findViewById(R.id.test_layout_option4);
		btnOption4.setTypeface(font1);
		tvQuestion = (TextView) findViewById(R.id.test_layout_question);
		tvQuestion.setTypeface(font1);

		btnOption1.setOnClickListener(this);
		btnOption2.setOnClickListener(this);
		btnOption3.setOnClickListener(this);
		btnOption4.setOnClickListener(this);

		mAlertDialog = new AlertDialog.Builder(TestActivity.this)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						loadQuestion();
						mAlertDialog.dismiss();
					}
				}).create();
		btnStart.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loadQuestion();
			}
		});

		spnUnit.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				mAllVocabs = mVocabularyBus.getVocabByUnit(spnUnit
						.getSelectedItem().toString());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}

	public void initChild() {
		mUnitBus = new UnitBus(TestActivity.this);
		mVocabularyBus = new VocabularyBus(TestActivity.this);
		font1 = new GlobalFont(getApplicationContext()).getUVNBanTay();
	}

	public void loadUnit() {
		try {
			UnitEntity[] units = mUnitBus.getAllUnit();
			String[] unitnames = new String[units.length];
			for (int i = 0; i < units.length; i++) {
				unitnames[i] = units[i].getName();
			}
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, unitnames);
			adapter.setDropDownViewResource(android.R.layout.select_dialog_item);
			spnUnit.setAdapter(adapter);
		} catch (Exception e) {
			Log.e("ThuanPQ", e.getMessage().toString());
		}
	}

	public void loadQuestion() {

		currentVocab = getVocabInUnitByRate(mAllVocabs);

		strAnswer = currentVocab.getMean();

		mRandomVocabs = mVocabularyBus.getVocabByRandom(currentVocab, 3);

		if (mRandomVocabs != null && mRandomVocabs.length == 3) {

			tvQuestion.setText(currentVocab.getWord());

			int mRadom = (int) (Math.random() * 4);

			switch (mRadom) {

			case 0:
				btnOption1.setText(currentVocab.getMean());
				btnOption2.setText(mRandomVocabs[0].getMean());
				btnOption3.setText(mRandomVocabs[1].getMean());
				btnOption4.setText(mRandomVocabs[2].getMean());
				break;

			case 1:
				btnOption2.setText(currentVocab.getMean());
				btnOption1.setText(mRandomVocabs[0].getMean());
				btnOption3.setText(mRandomVocabs[1].getMean());
				btnOption4.setText(mRandomVocabs[2].getMean());
				break;

			case 2:
				btnOption3.setText(currentVocab.getMean());
				btnOption1.setText(mRandomVocabs[0].getMean());
				btnOption2.setText(mRandomVocabs[1].getMean());
				btnOption4.setText(mRandomVocabs[2].getMean());
				break;

			case 3:
				btnOption4.setText(currentVocab.getMean());
				btnOption1.setText(mRandomVocabs[0].getMean());
				btnOption2.setText(mRandomVocabs[1].getMean());
				btnOption3.setText(mRandomVocabs[2].getMean());
				break;

			default:
				break;
			}
		} else {
			Toast.makeText(TestActivity.this,
					getString(R.string.test_layout_warming), Toast.LENGTH_SHORT)
					.show();
		}
	}

	public void addPoint(VocabEntity entity, boolean isright) {
		if (isright) {

			right++;

			tvRight.setText(Integer.toString(right));

			entity.setRight(entity.getRight() + 1);
		} else {

			wrong++;

			tvWrong.setText(Integer.toString(wrong));

			entity.setWrong(entity.getWrong() + 1);
		}

		mVocabularyBus.updateVocabByWord(entity);
	}

	public void initHeader() {

		Header header = new Header(this);

		header.setTextTitle(getString(R.string.test_layout));

		header.getImvMenu().setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	public VocabEntity getVocabInUnitByRate(VocabEntity[] entities) {

		int[] weightHolder = null;

		int weightCounter = 0;

		int intRandom = 0;

		weightHolder = new int[entities.length];

		for (int i = 0; i < entities.length; i++) {

			weightHolder[i] = (int) (((double) entities[i].getWrong() / entities[i]
					.getRight()) * 1000) + weightCounter;

			weightCounter = weightHolder[i];
		}

		intRandom = (int) (Math.random() * weightCounter);

		for (int j = 0; j < weightHolder.length; j++) {

			if (weightHolder[j] >= intRandom) {

				return entities[j];
			}
		}
		return null;
	}

	@Override
	public void onClick(final View v) {
		if (strAnswer.equalsIgnoreCase(""))
			return;

		AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
		mBuilder.setMessage(getString(R.string.test_layout_confirm_message));
		mBuilder.setTitle(getString(R.string.test_layout_confirm_title));
		mBuilder.setPositiveButton(getString(R.string.test_layout_confirm_yes),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						if (((Button) v).getText().toString()
								.equalsIgnoreCase(strAnswer)) {

							addPoint(currentVocab, true);

							loadQuestion();
						} else {

							mAlertDialog.setMessage(currentVocab.getWord()
									+ ": " + currentVocab.getMean());

							mAlertDialog
									.setIcon(android.R.drawable.ic_menu_close_clear_cancel);

							mAlertDialog.show();

							addPoint(currentVocab, false);
						}
					}
				});
		mBuilder.setNegativeButton(
				getString(R.string.test_layout_confirm_cancel),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				});
		mBuilder.create();
		mBuilder.show();
	}
}
